package com.bieyang.anjia.sqlitedatabase;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 本地数据库
 * 
 * @author M.y.q
 * 
 */
public class AnjiaSQLiteOpenHelper extends SQLiteOpenHelper {
	/**
	 * 数据库名称
	 */
	public static String dbName = "anjiaSql.db";
	/**
	 * 数据库版本
	 */
	public static int version = 3;

	public AnjiaSQLiteOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.e("oldVersion", "oldVersion=" + oldVersion + "----newVersion"
				+ newVersion);
		switch (oldVersion) {
		case 1:
			updateTable(db);
			break;
		case 2:
			updateTable(db);
			break;
		default:
			break;
		}

	}

	private void updateTable(SQLiteDatabase db) {
		db.beginTransaction();// 开始事务
		try {
			db.execSQL("drop table alarminfo");
			db.execSQL("drop table newsinfo");
			createTable(db);
			db.setTransactionSuccessful(); // 设置事务成功完成
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}
	}

	private void createTable(SQLiteDatabase db) {
		db.execSQL("create table "
				+ "alarminfo"
				+ " (_id integer,infoid integer primary key,uuid text,alarmmethod text,memberid text,"
				+ "alarmtype text,urlpath text,filepath text,filetype text,alarmtime text,alarmid integer,name text,isnew text,isdelete text)");
		db.execSQL("create table "
				+ "newsinfo"
				+ " (_id integer,newsid integer primary key,pictureurl text,"
				+ "picturepath text,title text,weburl text,submittime text,content text,isnew text,isdelete text)");
	}
}
